package com.watson.nettydemo.demo01;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * @author watson
 * @date 2021/7/21 17:30
 * @Description: 管道初始化器
 **/
public class SomeChannelInitializer extends ChannelInitializer<SocketChannel> {
    //当Channel 初始创建完毕后就会触发该方法的执行，用于初始化Channel
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        //从Channel中获取pipeline，创建channel的时候会一起创建
        ChannelPipeline pipeline = ch.pipeline();
        //将HttpServerCodec处理器放入到pipeline的最后
        //HttpServerCodec是HttpRequestDecoder和 HttpResponseEncoder的复合体
        //HttpRequestDecoder：http请求解码器，将Channel中的ByteBuf数据解码为HttpRequest对象
        //HttpResponseEncoder：http响应编码器，将httpResponse对象编码为将要在channel中发送的ByteBuf数据
        pipeline.addLast("HttpServerCodec", new HttpServerCodec());
//        pipeline.addLast(new HttpServerCodec());
        //将自定义的处理器放入到pipeline的最后
        pipeline.addLast(new SomeServerHandler());

    }
}
